package com.jzwl.common.upload.service;

import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.base.Splitter;
import com.jzwl.common.constant.Constants;
import com.jzwl.common.qiniu.QiniuUploadService;

@Service("uploadImgService")
public class UploadImgService {
	@Autowired
	private Constants constants;

	/**
	 * (文件上传)
	 * @Date 2018年1月18日
	 * @author gyp
	 * @param type
	 * @param img
	 * @return
	 * @throws Exception(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public String uploadImg(int type, MultipartFile img) throws Exception {

		try {
			InputStream inStream = img.getInputStream();
			String filename = URLEncoder.encode(img.getOriginalFilename(), "utf-8");
			List<String> result = Splitter.on(".").trimResults().splitToList(filename);
			QiniuUploadService u = new QiniuUploadService();
			String str = u.uploadImage(inStream, result.get(1), "");
			return constants.getSftz_image_path() + str;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
